package com.rocksdata.mc.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

/**
 * Created by 陈堂正 on 2017/8/15.
 */

public class TranscriptDBManager {
    private static TranscriptDBManager instance = null;
    private SQLiteDatabase db;

    private TranscriptDBManager(){
        this.db = DatabaseManager.getDatabase();
    }

    static synchronized TranscriptDBManager getInstance(){
        if(instance == null){
            instance = new TranscriptDBManager();
        }
        return instance;
    }

    /**
     * 保存成绩单单月数据
     * @param dateMillis 日期毫秒数，由yyyy-MM格式转换得到
     * @param jsonData 成绩信息json包
     */
    public synchronized void saveTranscriptInfo(long dateMillis, String jsonData){
        if(dateMillis == 0 || TextUtils.isEmpty(jsonData)) return;

        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.TTranscriptInfo.DATE_MILLIS, dateMillis);
        values.put(DatabaseHelper.TTranscriptInfo.JSON_DATA, jsonData);

        db.insert(DatabaseHelper.TUserInfo.TABLE_NAME, null, values);
    }

    /**
     * 获取成绩单数据
     * @param dateMillis 日期毫秒数，由yyyy-MM格式转换得到
     * @return
     */
    public synchronized String getTranscriptInfo(long dateMillis){
        Cursor c = db.query(DatabaseHelper.TTranscriptInfo.TABLE_NAME, null,
                DatabaseHelper.TTranscriptInfo.DATE_MILLIS + "=?",
                new String[]{String.valueOf(dateMillis)},
                null, null, null);
        try {
            if (c.moveToFirst()) {
                return c.getString(c.getColumnIndex(DatabaseHelper.TTranscriptInfo.JSON_DATA));
            }
        }catch (Exception e){

        }finally{
            c.close();
        }

        return null;
    }
}
